package br.com.santana.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.maplink2.webservices.Address;
import br.com.maplink2.webservices.City;
import br.com.maplink2.webservices.Point;
import br.com.santana.route.IRoute;
import br.com.santana.route.RouteImpl;
import br.com.santana.route.to.RouteTO;


@WebServlet("/RouteServlet")
public class RouteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public RouteServlet() {
        super();
   }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		System.setProperty("http.proxyHost", "proxysor.abnote.int");
		System.setProperty("http.proxyPort", "8080");
				
		String originCity = request.getParameter("originCity");
		String originState = request.getParameter("originState");
		String originStreet = request.getParameter("originStreet");
		String originNumber = request.getParameter("originNumber");
		
		String destinationCity = request.getParameter("destinationCity");
		String destinationState = request.getParameter("destinationState");
		String destinationStreet = request.getParameter("destinationStreet");
		String destinationNumber = request.getParameter("destinationNumber");
		 
		Address origin = setAddress(originCity, originState, originStreet, originNumber);
		Address destination = setAddress(destinationCity, destinationState, destinationStreet, destinationNumber);
		 
		IRoute route = new RouteImpl();
		Point originPoint = route.getCoordinatesByAddress(origin);
		Point destinationPoint = route.getCoordinatesByAddress(destination);
		
		RouteTO routTO = new RouteTO();;
		routTO.setOrigin(originPoint);
		routTO.setDestination(destinationPoint);
		
		
		routTO = route.getRouteValues(routTO);
		
		request.setAttribute("totalDistance", routTO.getTotalDistance());
		request.setAttribute("totalTime", routTO.getTotalTime());
		request.setAttribute("totalToll", routTO.getTotalToll());
		request.setAttribute("totalFuel", routTO.getTotalFuel());

		RequestDispatcher rd = getServletContext().getRequestDispatcher("/result.jsp");
		rd.forward(request, response);
	}
	
	public Address setAddress(String city, String state, String street, String number) {
		City cityAddress = new City();
		cityAddress.setName(city);
		cityAddress.setState(state);
		 
		Address address = new Address();
		address.setStreet(street);
		address.setHouseNumber(number);
		address.setCity(cityAddress);
		
		return address;
	}
	

}
